package cn.itsource.pethome.pet.service.impl;

import cn.itsource.pethome.Basic.domain.Point;
import cn.itsource.pethome.Basic.service.Impl.BaseServiceImpl;
import cn.itsource.pethome.Basic.util.CodeGenerateUtils;
import cn.itsource.pethome.Basic.util.DistanceUtil;
import cn.itsource.pethome.Basic.util.SystemContext;
import cn.itsource.pethome.order.domain.PetAcquisitionOrder;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.mapper.ShopMapper;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.domain.PetDetail;
import cn.itsource.pethome.pet.domain.SearchMasterMessage;
import cn.itsource.pethome.pet.mapper.PetAcquisitionOrderMapper;
import cn.itsource.pethome.pet.mapper.PetDetailMapper;
import cn.itsource.pethome.pet.mapper.PetMapper;
import cn.itsource.pethome.pet.mapper.SearchMasterMapper;
import cn.itsource.pethome.pet.service.ISearchMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SearchMasterServiceImpl extends BaseServiceImpl<SearchMasterMessage> implements ISearchMasterService {
    @Autowired
    private SearchMasterMapper searchMasterMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetAcquisitionOrderMapper orderMapper;
    /**
     * 发布寻主
     * @param searchMaster
     */
    @Override
    public void publish(SearchMasterMessage searchMaster) {
        //将字符串地址转换为经纬度
        Point point = DistanceUtil.getPoint(searchMaster.getAddress());
        //获取最近商铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAllShop());
        searchMaster.setShop(nearestShop);
        searchMasterMapper.add(searchMaster);
    }

    /**
     * 处理寻主信息
     * @param pet
     */
    @Override
    public void handlePendingMessage(Pet pet) {
        //根据寻主id获取寻主信息
        SearchMasterMessage searchMasterMessage = searchMasterMapper.findById(pet.getSearchMasterMessage().getId());
        //将寻主信息状态设为已处理
        searchMasterMessage.setState(1);
        searchMasterMapper.update(searchMasterMessage);
        //在将寻主信息设置回去
        pet.setSearchMasterMessage(searchMasterMessage);
        //保存pet
        petMapper.add(pet);
        //设置详情
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPet(pet);
        //保存宠物详情
        petDetailMapper.add(petDetail);
        //创建收购订单
        PetAcquisitionOrder order = createOrder(pet, searchMasterMessage);
        //保存收购订单
        orderMapper.add(order);

    }

    /**
     * 收购订单生成
     * @param pet
     * @param searchMasterMessage
     * @return
     */
    public PetAcquisitionOrder createOrder(Pet pet, SearchMasterMessage searchMasterMessage) {
        //制定宠物收购订单
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        //设置摘要
        order.setDigest("[收购单],对"+pet.getName()+"神兽收购");
        //设置状态 待报账
        //设置收购价
        if(pet.getCostprice().equals(0)){
            //完成
            order.setState(3);
        }else{
            order.setState(1);
        }
        order.setPrice(pet.getCostprice());
        //设置订单编号
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(searchMasterMessage.getUser().getId()));
        //设置支付方式
        order.setPaytype(0);
        //设置宠物信息
        order.setPet(pet);
        //设置用户信息
        order.setUser(searchMasterMessage.getUser());
        //设置商铺
        order.setShop(searchMasterMessage.getShop());
        //设置地址
        order.setAddress(searchMasterMessage.getAddress());
        //设置商铺员工
        order.setEmployee(SystemContext.getLogin());
        return order;
    }
}
